// Latte forum model base on MongoDB
// for CREATE operation ,when input unvailied throw exception

function Factory(){
    this.post = function(ownner=null,title='',content='',topic='',nickname='',tags=[],replies=[],date=new Date(),edited_date=null,viewed=0,reply_n=0,star=0,hidden=false){
        if(!ownner){
            throw 'latte: need set ownner'
        }else{
            title = title.slice(0,60);
            content = content.slice(0,5500);
            return {
                ownner,title,content,topic,nickname,tags,replies,date,edited_date,viewed,reply_n,star,hidden
            }
        }
    };

    this.reply = function(ownner=null,content='',post_id=null,nickname='',discuz=[],date=new Date(),hidden=false){
        if(!ownner || !post_id || !content){
            throw 'latte: need set ownner'
        }else{
            content = content.slice(0,750);
            return {
                ownner,content,post_id,nickname,discuz,date,hidden
            }
        }
    };
    
    this.user = function(account='',pwd='',nickname='',email='',avatar='',posts=[],replies=[],star_list=[],group=100,date=new Date(),near_login=null,hidden=false){
        if(account&&pwd){
            if(account.length>50 || pwd.length>50){
                throw 'Latte : account or password too long'
            }else if( account.length<5 || pwd.length<5 ){
                throw 'Latte : account or passsword too short'
            }else{
                email = email.slice(0,60);
                avatar = avatar.slice(0,500);
                nickname = nickname.slice(0,60);
                if(!nickname){ nickname = 'someone'};
                return {
                    account,pwd,nickname,email,avatar,posts,replies,star_list,group,date,near_login,hidden
                }
            }
        }else{
            throw 'Latte: need set account and pwd'
        }
    };
};

module.exports = new Factory()
